var base_url = "http://activity.gzl.com.cn";

function isLogined(toUrl) { 
    alert(toUrl);
    var loginUrl = "http://weixin.gzl.cn/account/login.html?type=1&goToUrl=" + toUrl;
    var usr_sign = getQueryString("usr_sign");
    var usr_nonce = getQueryString("usr_nonce");
    var usr_ts = getQueryString("usr_ts");
    var usr_info = getQueryString("wxUserInfo");
    var login_stamp = parseInt(new Date().getTime() / 1000);
    var checkToken = {
        usr_sign: usr_sign,
        usr_nonce: usr_nonce,
        usr_ts: usr_ts,
        usr_info: usr_info,
        login_stamp: login_stamp
    };
    alert('loginUrl=='+loginUrl);
    //已经登录过
    if(usr_sign&&usr_nonce&&usr_ts&&usr_info&&localStorage.check_token==undefined){
        localStorage.check_token = JSON.stringify(checkToken);
    }else{
        try{
            var check_token = JSON.parse(localStorage.check_token);
        }catch(err) {
            localStorage.removeItem("check_token");
            weui.alert("您还没有登录，请登录后再参与此活动！", function () {
                window.location = loginUrl;
            });
        }
        if(localStorage.check_token==undefined||check_token.usr_sign==undefined||check_token.usr_nonce==undefined||check_token.usr_ts==undefined||check_token.login_stamp==undefined||check_token.usr_info==undefined){   
            localStorage.removeItem("check_token");
            weui.alert("您还没有登录，请登录后再参与此活动！", function () {
                window.location = loginUrl;
            });
        }else{
            if(checkToken.usr_sign!=null){
                if(checkToken.usr_sign==check_token.usr_sign){
                    var this_stamp = parseInt(new Date().getTime() / 1000);
                    var time_min = parseInt(this_stamp-check_token.login_stamp)/ 3600;  // 
                    if(time_min>2){
                        window.location = loginUrl;
                    }
                }else{
                    localStorage.setItem("check_token" , JSON.stringify(checkToken) );
                }
            }else{
                var this_stamp = parseInt(new Date().getTime() / 1000);
                var time_min = parseInt(this_stamp-check_token.login_stamp)/ 3600;  // 
                if(time_min>2){
                    window.location = loginUrl;
                }
            }
        }
    }
}



//抽奖机会
function getluckyticket(activityId, channel, callback,) {
    var that = this;
    var check_token = JSON.parse(localStorage.check_token);
    var lucky_url = base_url + "/getTicket/" + activityId;
    $.ajax({
        type:'GET',
        contentType: false,
        dataType:'json',
        data: {
            channel : channel,
            usr_sign: check_token.usr_sign,
            usr_nonce: check_token.usr_nonce,
            usr_ts: check_token.usr_ts
        },
        url:lucky_url,
        success:function(data){
            if(data.message == "不能参与此抽奖活动"){
                $('.unit').text("谢谢惠顾");
                weui.alert("每天只能抽奖3次喔，请明天再来！");
            }else if(data.message == "抽奖时发生系统异常"){
                weui.alert("系统繁忙！请稍后重试！");
            }else{
                that.getlucky(data.data, activityId, channel, callback)
            }
        },
        error:function(){

        }
    });
}

//抽奖
function getlucky(data, activityId, channel, callback){
    var check_token = JSON.parse(localStorage.check_token);
    var lucky_url = base_url + "/play/"+ activityId +"?channel="+channel+"&nonce=" + data.nonce + "&timestamp=" + data.timestamp + "&token=" + data.token + "&usr_sign=" + encodeURIComponent(check_token.usr_sign) + "&usr_nonce=" + check_token.usr_nonce + "&usr_ts=" + check_token.usr_ts;
    $.ajax({
        type:'POST',
        url:lucky_url,
        dataType:'json',
        data: JSON.stringify({"actiDynamicParams":''}),
        contentType: 'application/json',
        processData: false,
        success:function(data){
            console.log('getlucky-success:'+data.data);
            if(data.message == "抽奖时发生系统异常"){
                weui.alert("系统繁忙！请稍后重试！");
            }else{
                callback(data.data);                    
            }
        },
        error:function(){
        }
    });
}

//获取url参数
function getQueryString(name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
    var r = window.location.search.substr(1).match(reg);   //返回要匹配的参数
    return r?decodeURIComponent(r[2]):null;
}






